
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>消息中心</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../style/css/admin.css" media="all">
</head>
<style>
  .layui-card-body {
    left: 20px;
    height: 30px;
  }

  .layui-table-cell{
    text-align:center;
    height: 35px;
  }
</style>
<body>

  <div class="layui-fluid" id="LAY-app-message">
    <div class="layui-card">
      <div class="layui-tab layui-tab-brief" lay-filter="message">
        <!-- 选项卡标题 -->
        <ul class="layui-tab-title">
          <li class="layui-this">文章审核<span class="layui-badge" id="layui-tab-article">6</span></li>
          <li>活动审核<span class="layui-badge" id="layui-tab-activity">6</span></li>
        </ul>
        <!-- 不同标题对应主题内容 layui-show表示是否显示 -->
        <div class="layui-tab-content">
        
          <div class="layui-tab-item layui-show">
            <!-- <div class="LAY-app-message-btns" style="margin-bottom: 10px;">
              <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="all" data-events="del">删除</button>
              <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="all" data-events="ready">标记已读</button>
              <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="all" data-events="readyAll">全部已读</button>
            </div> -->
            
            <!-- 表格操作列模板 -->
            <script type="text/html" id="auditBar">
              <button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="agree">同意</button>
              <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="reject">拒绝</button>
            </script>
            <table id="LAY-app-message-article" lay-filter="LAY-app-message-article"></table>
          </div>

          <div class="layui-tab-item">
          
            <!-- <div class="LAY-app-message-btns" style="margin-bottom: 10px;">
              <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="notice" data-events="del">删除</button>
              <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="notice" data-events="ready">标记已读</button>
              <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="notice" data-events="readyAll">全部已读</button>
            </div> -->

            <!-- 表格操作列模板 -->
            <script type="text/html" id="auditBar">
              <button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="agree">同意</button>
              <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="reject">拒绝</button>
            </script>

            <table id="LAY-app-message-activity" lay-filter="LAY-app-message-activity"></table>
            
          </div>
          
        </div>
      </div>
    </div>
  </div>
  <script src="./JS/utils/request.js"></script>
  <script src="./JS/utils/auth.js"></script>
  <script src="../layui/layui.all.js"></script>  
  <script>
  layui.use(['element', 'table', 'jquery'],function(){
    var $ = layui.jquery
    var element = layui.element;
    var table = layui.table;
    const token = localStorage.getItem('token')
       
    
     // 初始化文章审核表格（默认显示）
  var articleTable = table.render({
    elem: '#LAY-app-message-article',
    url: '/articles/AuditList', // 文章审核数据接口
    cols: [[
      { type: 'checkbox' },
      { title: '序号', type: 'numbers', autoSort: true },
      { field: 'title', title: '文章标题',templet: function(d) {
    return '<a href="articleedit.html?articleid=' + d.articleId + '">' + d.title + '</a>';
  }},
      { field: 'username', title: '作者' },
      { field: 'publishTime', title: '提交时间' },
      { field: 'status', title: '审核状态',templet: function(d){
                      const statusMap = {
                          0: '<span style="color:#999">草稿</span>',
                          1: '<span style="color:#FFE153">待审核</span>',
                          2: '<span style="color:#ffb800">已发布</span>',
                          3: '<span style="color:#FF2D2D">未通过</span>',
                      }
                      return statusMap[d.status] || '未知状态';
                  } },
      { title: '操作', toolbar: '#auditBar',width: 150}
    ]],
    headers: {
       'Authorization': token
  },
    page: true,
    parseData: function (res) { //res 即为原始返回的数据
          // console.log(res)
          return {
            "code": res.code, //解析接口状态
            "msg": res.msg, //解析提示文本
            "count": res.data.totalCount, //解析数据长度
            "data": res.data.list //解析数据列表
          }
        },
    done:function(res){
       console.log(res);
       $('#layui-tab-article').text(res.count);
        // 存储到 localStorage
        localStorage.setItem('article_count', res.count);
    }
  });

  // 初始化活动审核表格（默认隐藏）
  var activityTable = table.render({
    elem: '#LAY-app-message-activity',
    url: '/activities/AuditList', // 活动审核数据接口
    cols: [[
      { type: 'checkbox' },
      { title: '序号', type: 'numbers', autoSort: true },
      {  title: '活动标题',templet: function(d) {
        console.log(d);
        
    return '<a href="activityedit.html?id=' + d.activityId + '">' + d.title + '</a>';
  }},
      { field: 'username', title: '主办方' },
      { field: 'submitTime', title: '提交时间' },
      { field: 'status', title: '审核状态',templet: function(d){
                      const statusMap = {
                          0: '<span style="color:#FFE153">审核中</span>',
                          1: '<span style="color:#999">未开始</span>',
                          2: '<span style="color:#ffb800">正在进行</span>',
                          3: '<span style="color:#5fb878">已结束</span>',
                          4:'<span style="color:#999">已取消</span>'
                      }
                      return statusMap[d.status] || '未知状态';
                  }},
      { title: '操作', toolbar: '#auditBar',width: 150}
    ]],
    page: true,
    parseData: function (res) { //res 即为原始返回的数据
          // console.log(res)
          return {
            "code": res.code, //解析接口状态
            "msg": res.msg, //解析提示文本
            "count": res.data.totalCount, //解析数据长度
            "data": res.data.list //解析数据列表
          }
        },
        headers: {
       'Authorization': token
  },
    done: function (res) {
      console.log(res);
       $('#layui-tab-activity').text(res.count);
       // 存储到 localStorage
       localStorage.setItem('activity_count', res.count);
      // $('#LAY-app-message-activity').hide(); // 初始隐藏活动表格
    }
  });

  // 监听活动审核同意或拒绝工具条操作
  table.on('tool(LAY-app-message-activity)', function(obj){
    var data = obj.data; // 获得当前行数据
    var event = obj.event; // 获得 lay-event 对应的值
    console.log(data);
    console.log(event);
    
    
    if(event === 'agree'){
      handleAudit(data.activityId,data.startTime,data.endTime,data.feedback,'approve');
    } else if(event === 'reject'){
      handleAudit(data.activityId,data.startTime,data.endTime,data.feedback,'reject');
    }
  
  });

  // 监听文章审核同意或拒绝工具条操作
  table.on('tool(LAY-app-message-article)', function(obj){
    var data = obj.data; // 获得当前行数据
    var event = obj.event; // 获得 lay-event 对应的值
    console.log(data);
    console.log(event);
    
    
    if(event === 'agree'){
      handleArticle(data.articleId,data.feedback,'approve');
    } else if(event === 'reject'){
      handleArticle(data.articleId,data.feedback,'reject');
    }
  
  });

  
// 统一处理审核操作
function handleAudit(activityId,startTime,endTime,feedback,action) {
    const actionText = action === 'approve' ? '同意' : '拒绝';
    console.log(startTime,endTime);
    

    if (action === 'approve') {
        // 同意操作
        layer.confirm(`确定要${actionText}该申请吗？`, { title: '操作确认' }, function (index) {
            $.ajax({
                url: `/activities/${action}`,
                type: 'PUT',
                contentType: 'application/json',
                data: JSON.stringify({ 
                    activityId: activityId, 
                    startTime:startTime,
                    endTime:endTime,
                }),
                headers:{
              'Authorization':localStorage.getItem('token')
    },
                success: function (res) {
                    handleResponse(res, actionText);
                },
                error: handleError
            });
            layer.close(index);
        });
    } else {
        // 拒绝操作
        layer.open({
            type: 1,
            title: '请输入拒绝原因',
            area: ['500px', '250px'],
            content: '<textarea id="feedback" placeholder="请输入详细的拒绝理由..." class="layui-textarea" style="margin: 10px;"></textarea>',
            btn: ['确认拒绝', '取消'],
            yes: function (index) {
                const feedback = $('#feedback').val().trim();
                if (!feedback) {
                    layer.msg('拒绝原因不能为空', { icon: 2 });
                    return;
                }

                $.ajax({
                    url: `/activities/${action}`,
                    type: 'PUT',
                    contentType: 'application/json',
                    data: JSON.stringify({
                        activityId: activityId,
                        feedback: feedback
                    }),
                    headers:{
              'Authorization':localStorage.getItem('token')
    },
                    success: function (res) {
                        handleResponse(res, actionText);
                        layer.close(index);
                    },
                    error: handleError
                });
            }
        });
    }
}

// 统一处理审核操作
function handleArticle(articleId,feedback,action) {
    const actionText = action === 'approve' ? '同意' : '拒绝';
    
    

    if (action === 'approve') {
        // 同意操作
        layer.confirm(`确定要${actionText}该申请吗？`, { title: '操作确认' }, function (index) {
            $.ajax({
                url: `/articles/${action}`,
                type: 'PUT',
                contentType: 'application/json',
                data: JSON.stringify({ 
                    articleId: articleId, 
                    
                }),
                headers:{
              'Authorization':localStorage.getItem('token')
    },
                success: function (res) {
                    handleArticleResponse(res, actionText);
                },
                error: handleArticleError
            });
            layer.close(index);
        });
    } else {
        // 拒绝操作
        layer.open({
            type: 1,
            title: '请输入拒绝原因',
            area: ['500px', '250px'],
            content: '<textarea id="feedback" placeholder="请输入详细的拒绝理由..." class="layui-textarea" style="margin: 10px;"></textarea>',
            btn: ['确认拒绝', '取消'],
            yes: function (index) {
                const feedback = $('#feedback').val().trim();
                if (!feedback) {
                    layer.msg('拒绝原因不能为空', { icon: 2 });
                    return;
                }

                $.ajax({
                    url: `/articles/${action}`,
                    type: 'PUT',
                    contentType: 'application/json',
                    data: JSON.stringify({
                        articleId: articleId,
                        feedback: feedback
                    }),
                    headers:{
              'Authorization':localStorage.getItem('token')
    },
                    success: function (res) {
                      handleArticleResponse(res, actionText);
                        layer.close(index);
                    },
                    error: handleArticleError
                });
            }
        });
    }
}

// 统一处理成功响应
function handleResponse(res, actionText) {
    if (res.code === 0) {
        layer.msg(`${actionText}操作成功`, { icon: 1 });
        table.reload('LAY-app-message-activity', { page: { curr: 1 } }); // 刷新并回到第一页
    } else {
        layer.msg(res.msg || `${actionText}操作失败`, { icon: 2 });
    }
    
}

// 统一处理成功响应
function handleArticleResponse(res, actionText) {
    if (res.code === 0) {
        layer.msg(`${actionText}操作成功`, { icon: 1 });
        table.reload('LAY-app-message-article', { page: { curr: 1 } }); // 刷新并回到第一页
    } else {
        layer.msg(res.msg || `${actionText}操作失败`, { icon: 2 });
    }
    
}

// 统一处理错误
function handleError(xhr) {
    layer.msg(`请求失败: ${xhr.statusText} (${xhr.status})`, { icon: 2 });
    table.reload('LAY-app-message-activity');
}

// 统一处理错误
function handleArticleError(xhr) {
    layer.msg(`请求失败: ${xhr.statusText} (${xhr.status})`, { icon: 2 });
    table.reload('LAY-app-message-article');
}





  });
  </script>
</body>
</html>